﻿#include "inputfunction.h"
//#include <TSettings>
#include "common/tsettings/tsettings.h"
#include "common/common.h"
#include <QDebug>
#include "outputfunction.h"
#include "myserialport/spscaleadaptor.h"



const int InputFunction::mInputNoTab[]=
{
    0,1,2,3,4,5,6,7,8,9,10,11
};


#define INPUT_IO_COUNTS (sizeof(mInputNoTab)/sizeof(int))

#define FUNC_WRAPPER(x) &InputFunctionThread::x
const CalFun InputFunction::functionList[] =
{
    {0,                                  QT_TR_NOOP("无")},
//    {&InputFunction::zero,         QT_TR_NOOP("Zeroset scale")},
//    {&InputFunction::acqDelTare,   QT_TR_NOOP("Acq/Del tare")},
//    {&InputFunction::acquireTare,  QT_TR_NOOP("Acquire tare")},
//    {&InputFunction::deleteTare,   QT_TR_NOOP("Delete tare")},
//    {&InputFunction::weight,       QT_TR_NOOP("1st/2nd weitht")},
//    {&InputFunction::directWeight, QT_TR_NOOP("Direct weight")},
//    {&InputFunction::disableWeight,QT_TR_NOOP("Disable weight")},
    {&InputFunction::in,QT_TR_NOOP("IN")},
    {&InputFunction::out,QT_TR_NOOP("OUT")},
    {&InputFunction::positon1,QT_TR_NOOP("位置1")},
    {&InputFunction::positon2,QT_TR_NOOP("位置2")},
    {&InputFunction::positon3,QT_TR_NOOP("位置3")},
    {&InputFunction::positon4,QT_TR_NOOP("位置4")},
    {&InputFunction::positon5,QT_TR_NOOP("位置5")},
    {&InputFunction::positon6,QT_TR_NOOP("位置6")},
    {&InputFunction::positon7,QT_TR_NOOP("位置7")},
    {&InputFunction::positon8,QT_TR_NOOP("位置8")},
    {&InputFunction::positon9,QT_TR_NOOP("位置9")},
    {&InputFunction::positon10,QT_TR_NOOP("位置10")}
    //{&InputFunctionThread::functionStart,QT_TR_NOOP("Start")},
//    {&InputFunction::functionEnd,  QT_TR_NOOP("End")}
};

#define FUNCTION_COUNTS sizeof(functionList)/sizeof(CalFun)

const QString InputFunction::INPUT_SET="input";
const QString InputFunction::FUNCTION_SET="function";
const QString InputFunction::STATUS_NORMAL_SET="status_res";

const char* InputFunction::NORMAL_STATUS[]=
{
//    #if IN_OPEN == 1
    QT_TR_NOOP("常闭"),
    QT_TR_NOOP("常开")
//    #else
//    QT_TR_NOOP("Normal open"),
//    QT_TR_NOOP("Normal close")
//    #endif
};

InputFunction::InputFunction(QObject *parent) :
    QObject(parent)
{
    TSettings set(DIR_SETTINGS,TSettings::IniFormat);
    set.beginReadArray(INPUT_SET);
    for(int i=0;i<INPUT_IO_COUNTS;++i)
    {
        set.setArrayIndex(i);
        Input* in = new Input(i);
        mInputList<<in;
        uint fn = set.value(FUNCTION_SET,0).toUInt();
        uint normalstaust = set.value(STATUS_NORMAL_SET,NORMALCLOSE).toUInt();
        initInputFunction(in,normalstaust,normalstaust,fn);
    }
    set.endArray();
    connect(SPSCALEADAPTOR(),SIGNAL(inputCome(int,int)),this,SLOT(handleInput(int,int)));
    //this->start();
//    Input *io = mInputTab;

//    uint i=1;

//    for(;io->ionum!=-1;io++)
//    {
//        //qDebug()<<"+++++++++++++++++++++++++++++++++++++++++++++++++++";
//        set.beginGroup(INPUT_SET+QString::number(i++));

//        uint s = set.value(FUNCTION_SET,0).toUInt();
//        if(s>=FUNCTION_COUNTS) s = 0;
//        io->functionNum = s;
//        io->inputFunction = functionList[s].func;

//        io->normalStatus = set.value(STATUS_NORMAL_SET,IN_OPEN).toUInt();

//        set.endGroup();
//        gpio_init(io->ionum,IN);
//    }

    
}

void InputFunction::saveInputFunction(uint num,uint normalStatus,uint functionNum)
{
    if(num>=INPUT_IO_COUNTS) return;

    Input *in = mInputList.at(num);

    if(normalStatus>1) normalStatus=1;
    if(functionNum>=FUNCTION_COUNTS) functionNum = 0;
    TSettings set(DIR_SETTINGS,TSettings::IniFormat);
    set.beginWriteArray(INPUT_SET);
    set.setArrayIndex(num);
    set.setValue(FUNCTION_SET,functionNum);
    set.setValue(STATUS_NORMAL_SET,normalStatus);
    set.endArray();
    initInputFunction(in,normalStatus,normalStatus,functionNum);
}

void InputFunction::handleInput(int num, int val)
{
    if(num<0||num>INPUT_IO_COUNTS-1)
        return;
    if(val<0||val>1)
        return;
    Input* in = mInputList.at(num);
    if(in->getStatus()!=val)
    {
        in->setStatus((Input::Status)val);
    }
    else
    {
        return;
    }

    if(in->inputFunction!=0)
    {
        if(val!=in->getNormalStatus())
        {
            (this->*in->inputFunction)(true);
        }
        else
        {
            (this->*in->inputFunction)(false);
        }

    }
}

int InputFunction::inputIoCount()
{
    return INPUT_IO_COUNTS;
}

int InputFunction::functionCount()
{
    return FUNCTION_COUNTS;
}

QStringList InputFunction::functionNameList()
{
   QStringList list;
   for(uint i =0;i<FUNCTION_COUNTS;i++)
   {
       list<<tr(functionList[i].name);
   }
   return list;
}

QStringList InputFunction::normalStatusList()
{
   QStringList list;
   for(int i =0;i<2;i++)
   {
       list<<tr(NORMAL_STATUS[i]);
   }
   return list;
}

InputFunction *InputFunction::mInstance = NULL;

InputFunction *InputFunction::getInputFunctionMgr()
{
    if(mInstance==0)
        mInstance = new InputFunction();
    return mInstance;
}

void InputFunction::initInputFunction(Input* input, uint status,uint normalstatus, uint functionnum)
{
    input->inputFunction = functionList[functionnum].func;
    input->setStatus((Input::Status)status);
    input->setNormalStatus(normalstatus);
    input->setFunctionNum(functionnum);
    input->setFuncName(functionList[functionnum].name);
}

uint InputFunction::getNormalStatus(uint num)
{
    return mInputList.at(num)->getNormalStatus();
}

uint InputFunction::getFunctionNum(uint num)
{
    return mInputList.at(num)->getFunctionNum();
}

int InputFunction::getStatus(uint num)
{
//    int curr = gpio_read(mInputNoTab[num].ionum);
//    qDebug()<<"read"<<curr;
//    if(curr == mInputTab[num].normalStatus)
//        return 0;
//    else
//        return 1;
    return mInputList.at(num)->getStatus();
}



QString InputFunction::getInputSetting(uint i)
{
    QString str;
    TSettings set(DIR_SETTINGS,TSettings::IniFormat);



    if(i>=INPUT_IO_COUNTS) i = 0;



    set.beginGroup(INPUT_SET+QString::number(i+1));

    int s  = set.value(STATUS_NORMAL_SET,1).toInt();
    if(s<0||s>1) s=0;
    str += NORMAL_STATUS[s];

    str += " - ";
    s = set.value(FUNCTION_SET,0).toInt();
    if(s>=FUNCTION_COUNTS) s = 0;

    str += functionList[s].name;

    set.endGroup();
    return str;
}


